package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.adapter.CategoryListAdapter;
import com.eboss2.sbs.model.CategoryListInfo;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.views.MyListView;

import java.util.ArrayList;
import java.util.List;

public class MyCategoryTwoListActivity extends BaseActivity implements View.OnClickListener{
	
	private CategoryListInfo vo;
	private List<CategoryListInfo> categoryListInfos = new ArrayList<>();
	private MyListView listview;
	private CategoryListAdapter categoryListAdapter;
	private TextView tv_category_text;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_category_choice);
		MyActivityManager.getInstance().addActivity(this);
		initView();
//		testData();
	}

	private void testData() {
		for(int i = 0; i<5; i++){
			CategoryListInfo categoryListInfo = new CategoryListInfo();
			categoryListInfo.setId(i);
			categoryListInfo.setName(i+"1");
			categoryListInfos.add(categoryListInfo);
		}
	}

	private void initView(){
		RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
		relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "类目选择");
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		tv_category_text = textView(R.id.tv_category_text);
		tv_category_text.setText("二级类目");


		listview = findViewById(R.id.listview);
		categoryListAdapter = new CategoryListAdapter(mContext, categoryListInfos);
		listview.setAdapter(categoryListAdapter);
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
				vo = categoryListInfos.get(position);
				for (int i = 0; i < categoryListInfos.size(); i++){
					categoryListInfos.get(i).setShow(false);
				}
				vo.setShow(true);
				categoryListAdapter.notifyDataSetChanged();

				Bundle bundle = new Bundle();
				bundle.putString("twoCategory", vo.getName());
				bundle.putString("twoCategoryId", vo.getId()+"");
				setResult(RESULT_OK, (new Intent()).putExtra("twoCategory", bundle));
				finish();

			}
		});


		vo = (CategoryListInfo) getIntent().getSerializableExtra("two");

		if (vo != null && vo.getMallCategoryList() != null && vo.getMallCategoryList().size() > 0){
			categoryListInfos.clear();
			CategoryListInfo categoryListInfo = new CategoryListInfo();
			categoryListInfo.setId(0);
			categoryListInfo.setName("不选");
			categoryListInfos.add(categoryListInfo);
			categoryListInfos.addAll(1, vo.getMallCategoryList());
			categoryListAdapter.notifyDataSetChanged();
		}



	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			default: break;
		}
	}
	

	
}
